package com.oa.gform.server.ibatis;

import java.util.HashMap;

import com.oa.gform.client.service.IsRPCBean;

/***
 * Gestore dei mapping fra i mapper e i bean di ibatis
 * @author Marco
 *
 */
public class GIBatisRegistry {
	static HashMap<Class<? extends IsRPCBean<?>>, Class<? extends GIBatisMapper<?,?>>> mapperMap  = new HashMap<Class<? extends IsRPCBean<?>>, Class<? extends GIBatisMapper<?,?>>>();
	
	public static void addMapping(Class<? extends IsRPCBean<?>> bean,Class<? extends GIBatisMapper<?,?>> mapper) {
		mapperMap.put(bean, mapper);
	}
	
	@SuppressWarnings({ "unchecked" })
	public static Class<GIBatisMapper> getMapperFromBean(Class<? extends IsRPCBean> bean) {
		return (Class<GIBatisMapper>)mapperMap.get(bean);
	}
	
	public static GIBatisMapper<?,?> getMapperInstance(Class<? extends IsRPCBean<?>> bean) throws InstantiationException, IllegalAccessException {
		// TODO implementare cache
		return mapperMap.get(bean).newInstance();
	}
	
	
	public static void rebuild() {
		mapperMap  = new HashMap<Class<? extends IsRPCBean<?>>, Class<? extends GIBatisMapper<?,?>>>();
	}
	
	
}
